<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>底图切换</title>
  <script src="../../lib/include-openlayers-local.js"></script>
  <script src="https://lib.baomitu.com/jquery/3.6.0/jquery.js"></script>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    #map {
      width: 100vw;
      height: 100vh;
    }

    img {
      position: fixed;
      bottom: 20px;
      right: 10px;
      width: 200px;
      height: 139px;
      z-index: 9999;
    }

    #toggle1 {
      right: 300px;
    }
  </style>
</head>

<body>
  <img id="toggle1" src="./矢量.png" />
  <img id="toggle2" src="./影像.png" />
  <div id="map"></div>

  <script>
    /* 高德矢量图层 */
    var gaodeMapLayer = new ol.layer.Tile({
      title: '高德矢量图层',
      source: new ol.source.XYZ({
        url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
        wrapX: false,
      }),
    })
    /* 高德影像图层 */
    var image = new ol.layer.Tile({
      title: '高德影像图层',
      source: new ol.source.XYZ({
        url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=6&x={x}&y={y}&z={z}',
        wrapX: false,
      }),
    })
    /* 高德注记图层 */
    var zhuji = new ol.layer.Tile({
      title: '高德地图',
      source: new ol.source.XYZ({
        url: 'http://webst02.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scale=1&style=8',
        wrapX: false,
      }),
    })
    /* 使用ol加载高德图层 */
    const map = new ol.Map({
      target: 'map',
      layers: [image, gaodeMapLayer, zhuji],
      view: new ol.View({
        center: [114, 30],
        zoom: 6,
        projection: 'EPSG:4326',
      }),
    })

    var layers = map.getLayers().getArray()
    layers[2].setVisible(false)
    // 切换矢量图层
    $('#toggle1').click(() => {
      layers[0].setVisible(false)
      layers[1].setVisible(true)
      layers[2].setVisible(false)
    })
    // 切到影像图层
    $('#toggle2').click(() => {
      layers[1].setVisible(false)
      layers[0].setVisible(true)
      layers[2].setVisible(true)
    })
  </script>
</body>

</html>